package com.XiaoLongGroup.webtech.service.impl;/**
 * @author : SLCcccc
 * @date : 2023/4/11 14:15
 */

import com.XiaoLongGroup.webtech.entity.User;
import com.XiaoLongGroup.webtech.mapper.ArticleMapper;
import com.XiaoLongGroup.webtech.mapper.UserMapper;
import com.XiaoLongGroup.webtech.service.UserService;
import com.XiaoLongGroup.webtech.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 *@ClassName UserServiceimpl
 *@Description TODO
 *@Author SLCcccc
 *@Date 2023/4/11 14:15
 *@Version 1.0
 */
@Service
public class UserServiceimpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public List<User> getUserList() {
        return userMapper.getAllUserList();
    }

    @Override
    public User userLogin(User user) {
        return userMapper.getUserByUserIdAndPassword(user.getUserId(), user.getPassword());
    }

    @Override
    public boolean userLogon(User user) {
        // add
        try {
            if(userMapper.addUser(user) != 0) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    @Override
    public Page<User> getUserList(int page, int size) {
        Page<User> userPage = new Page<>(page, size);
        userPage.setRows(userMapper.getUserList((page - 1)*size, size));
        userPage.setTotal(userMapper.getCount(""));
        return userPage;
    }

    @Override
    public Page<User> getUserListByNameLike(String name, int page, int size) {
        Page<User> userPage = new Page<>(page, size);
        userPage.setRows(userMapper.getUserListByNameLike(name,(page - 1)*size, size));
        userPage.setTotal(userMapper.getCount(name));
        return userPage;
    }

    @Override
    public Boolean userInfoChange(User user) {
        try {
            if (userMapper.updateUser(user)!=0){
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    @Override
    public Boolean addUser(User user) {
//        // 封装属性
//        User user = new User();
//        user.setUserId(userId);
//        user.setPassword(password);
//        user.setEmail(email);
//        user.setPhone(phone);
//        user.setBirthday(birthday);
        // add
        try {
            if(userMapper.addUser2(user) != 0) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    @Override
    public User getUserById(String userId) {
        return userMapper.getUserByUserId(userId);
    }

    @Override
    public boolean deleteUserByUserId(String userId) {
        return userMapper.deleteUserByUserId(userId) != 0;
    }

    @Override
    public boolean pwdReset(User user) {
        User user1 = userMapper.getUserByUserId(user.getUserId());
        if(user1==null || !user1.getPhone().equals(user.getPhone())){
            return false;
        }
        user1.setPassword(user.getPassword());
        try {
            userMapper.updateUser(user1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return true;
    }

}
